package jfox.platform.aaa.entity;

import java.util.Arrays;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

import jfox.platform.common.Utils;
import jfox.platform.infrastructure.VersionableAndRefInspectableEntityObject;

/**
 * @author <a href="mailto:thaiguo@gmail.com">Taiguo Zhang</a>
 */
@Entity
@Table(name = "T_SYS_USER")
public class User extends VersionableAndRefInspectableEntityObject {

    @Column(name = "FIRST_NAME")
    private String firstName;

    @Column(name = "LAST_NAME")
    private String lastName;

    @Column(name = "PASSWORD")
    private String password;

    @Column(name = "RESTRICT_IP")
    private String restrictIp;

    @Column(name = "GENDER")
    private String gender;


    @Column(name = "BIRTHDAY")
    private String birthday;

    @Column(name = "EMAIL")
    private String email;

    @Column(name = "MOBILE")
    private String mobile;

    @Column(name = "PHONE")
    private String phone;

    @Column(name = "POST_ADDRESS")
    private String postAddress;

    @Column(name = "POST_CODE")
    private String postCode;

    @Column(name = "PICTURE")
    private String picture;

    @Column(name = "GRADUATE_SCHOOL")
    private String graduateSchool;

    @Column(name = "GRADUATE_YEAR")
    private Integer graduateYear;

    @Column(name = "EDUCATION_DEGREE")
    private Short educationDegree;

    @Column(name = "SPECIALITY")
    private Short speciality;

    @Column(name = "POSITION")
    private Short position;

    @Column(name = "LAST_LOGIN")
    private Long lastLogin;

    @Column(name = "LANGUAGE")
    private Short language;

    @Column(name = "THEME")
    private Short theme;

    @Column(name = "ROLES")
    private String roles;

    private long[] rolesArray;

    @Column(name = "NODES")
    private String nodes;

    @Column(name = "DEPARTMENT_ID")
    private Long departmentId;


    public static void main(String[] args) {

    }


    public String getFirstName() {
        return firstName;
    }


    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }


    public String getLastName() {
        return lastName;
    }


    public void setLastName(String lastName) {
        this.lastName = lastName;
    }


    public String getPassword() {
        return password;
    }


    public void setPassword(String password) {
        this.password = password;
    }


    public String getRestrictIp() {
        return restrictIp;
    }


    public void setRestrictIp(String restrictIp) {
        this.restrictIp = restrictIp;
    }


    public String getGender() {
        return gender;
    }


    public void setGender(String gender) {
        this.gender = gender;
    }


    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }


    public String getMobile() {
        return mobile;
    }


    public void setMobile(String mobile) {
        this.mobile = mobile;
    }


    public String getPhone() {
        return phone;
    }


    public void setPhone(String phone) {
        this.phone = phone;
    }


    public String getPostAddress() {
        return postAddress;
    }


    public void setPostAddress(String postAddress) {
        this.postAddress = postAddress;
    }


    public String getPostCode() {
        return postCode;
    }


    public void setPostCode(String postCode) {
        this.postCode = postCode;
    }


    public String getPicture() {
        return picture;
    }


    public void setPicture(String picture) {
        this.picture = picture;
    }


    public String getGraduateSchool() {
        return graduateSchool;
    }


    public void setGraduateSchool(String graduateSchool) {
        this.graduateSchool = graduateSchool;
    }


    public Integer getGraduateYear() {
        return graduateYear;
    }


    public void setGraduateYear(Integer graduateYear) {
        this.graduateYear = graduateYear;
    }


    public Short getEducationDegree() {
        return educationDegree;
    }


    public void setEducationDegree(Short educationDegree) {
        this.educationDegree = educationDegree;
    }


    public Short getSpeciality() {
        return speciality;
    }


    public void setSpeciality(Short speciality) {
        this.speciality = speciality;
    }


    public Short getPosition() {
        return position;
    }


    public void setPosition(Short position) {
        this.position = position;
    }


    public Long getLastLogin() {
        return lastLogin;
    }


    public void setLastLogin(Long lastLogin) {
        this.lastLogin = lastLogin;
    }


    public Short getLanguage() {
        return language;
    }


    public void setLanguage(Short language) {
        this.language = language;
    }


    public Short getTheme() {
        return theme;
    }


    public void setTheme(Short theme) {
        this.theme = theme;
    }


    public String getRoles() {
        return roles;
    }

    public long[] getRolesArray(){
        if(rolesArray == null) {
            rolesArray = Utils.string2Array(getRoles());
            Arrays.sort(rolesArray); // 排序
        }
        return rolesArray;
    }


    public void setRoles(String roles) {
        this.roles = roles;
    }


    public String getNodes() {
        return nodes;
    }


    public void setNodes(String nodes) {
        this.nodes = nodes;
    }


    public Long getDepartmentId() {
        return departmentId;
    }


    public void setDepartmentId(Long departmentId) {
        this.departmentId = departmentId;
    }


    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getBirthday() {
        return birthday;
    }

    public boolean hasRole(long roleId){
        return Arrays.binarySearch(getRolesArray(), roleId) >= 0;
    }
}
